home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks96 / ScottsSoul.sit / Scott's Soul / ShowIcon7.c < prev    next >
C/C++ Source or Header  |  1996-06-21  |  7KB  |  253 lines

  1. #include <Icons.h>
  2. #include <OSUtils.h>
  3. #include <Resources.h>
  4. #include <Memory.h>
  5. /*
  6.     ShowIcon7.c
  7.     
  8.     ShowINIT compatible routine that shows 'ICN#' and 'iclx' flavor icons.
  9.     For use by all INITs in System 7 and beyond.
  10.     
  11.     This code is based on Patrick C. Beard's ShowIconFamily
  12.     by Patrick C. Beard, which in turn was derived from the
  13.     original ShowInit by Paul Mercer, Darin Adler,
  14.     Paul Snively, and Steve Capps.
  15.     
  16.     Modified by James W. Walker for compatibility with IconWrap 1.2,
  17.     for use as a separate code resource, and to use the new System 7
  18.     icon-drawing routines.  This code is in the public domain.
  19.     
  20.     Addresses for James W. Walker: 76367,2271@compuserve.com or
  21.         walkerj@math.scarolina.edu
  22.     
  23.     Instructions for use:
  24.     
  25.         • Create a family of icons with ResEdit 2.1 or later.  This will
  26.             include 'ICN#', 'icl4', & 'icl8' icons, all with the
  27.             same resource ID.
  28.         
  29.         To use within a larger INIT project:
  30.             • #define SEPARATE_CODE 0  below.
  31.             • Call ShowIcon7( id, adv ) with the resource id of
  32.                 the family that you used.  The Boolean parameter adv
  33.                 indicates whether you want the next use of ShowIcon7
  34.                 to advance to a new position.  Normally you pass TRUE,
  35.                 but you can get an animated-icon effect by passing FALSE
  36.                 all except the last time you call ShowIcon7.
  37.         
  38.         To use as a separate code resource:
  39.             • #define SEPARATE_CODE 1  below.
  40.             • Set the project type to code resource, set the code type
  41.                 and resource ID (I use type 'Code', ID -4048 for cdevs),
  42.                 compile and merge into your INIT or cdev file.
  43.                 Set the resource type to Locked, so you won't have
  44.                 to call HLock when you use it.
  45.             • In your main INIT code, call ShowIcon7 like so:
  46.                 pascal void (*ShowIcon7)( short resid, Boolean adv );
  47.                 Handle  show_init;
  48.                 
  49.                 show_init = GetResource( 'Code', -4048 );
  50.                 ShowIcon7 = (pascal void (*)(short, Boolean))
  51.                             StripAddress( *show_init );
  52.                 ShowIcon7( iconID, advance );
  53.  */
  54.  
  55. #define SEPARATE_CODE    0
  56.  
  57.  
  58. #if SEPARATE_CODE
  59. #define        ShowIcon7        main    /* JWW */
  60. #endif
  61.  
  62. #ifndef nil
  63. #define nil ((void*)0L)
  64. #endif
  65.  
  66. #ifndef topLeft
  67.     #define topLeft(r) ((Point*)&r)[0])
  68. #endif
  69. #ifndef botRight
  70.     #define botRight(r) ((Point*)&r)[1])
  71. #endif
  72.  
  73. typedef struct myQDGlobals {
  74.     char privates[76];
  75.     long randSeed;
  76.     BitMap screenBits;
  77.     Cursor arrow;
  78.     Pattern dkGray;
  79.     Pattern ltGray;
  80.     Pattern gray;
  81.     Pattern black;
  82.     Pattern white;
  83.     GrafPtr thePort;
  84.     long    end;
  85. } myQDGlobals;
  86.  
  87.  
  88. /* prototypes */
  89. pascal void ShowIcon7(short iconId, Boolean advance);
  90. static void DrawBWIcon( short iconId, Rect *icon_rect, Boolean visible );
  91. static void Next_position( void );
  92. static void GetIconRect( register Rect* iconRect, Rect *screen_rect );
  93.  
  94. /* this is where it all happens. */
  95.  
  96. pascal void ShowIcon7( short iconId, Boolean advance )
  97. {
  98.     long    oldA5;
  99.     myQDGlobals qd;                /* our QD globals. */
  100.     SysEnvRec environment;        /* machine configuration. */
  101.     CGrafPort gp;                /* our grafport. */
  102.     Rect    icon_rect;
  103.     OSErr    err;
  104.     
  105.     /* get a value for A5, a structure that mirrors qd globals. */
  106.     oldA5 = SetA5((long)&qd.end);
  107.     InitGraf(&qd.thePort);
  108.     
  109.     /* find out what kind of machine this is. */
  110.     SysEnvirons(curSysEnvVers, &environment);
  111.  
  112.     GetIconRect( &icon_rect, &qd.screenBits.bounds );
  113.  
  114.     /*
  115.         The old IconWrap INIT patches CopyBits but does not know about
  116.         CopyMask or IconDispatch.  So in the color case, we draw the
  117.         black and white icon with an empty mask region.
  118.     */
  119.  
  120.     if ( (environment.systemVersion >= 0x0700) && environment.hasColorQD )
  121.     {
  122.         OpenCPort(&gp);
  123.         DrawBWIcon( iconId, &icon_rect, false );
  124.         err = PlotIconID( &icon_rect, atNone, ttNone, iconId );
  125.         CloseCPort(&gp);
  126.     }
  127.     else
  128.     {
  129.         OpenPort((GrafPtr)&gp);
  130.         DrawBWIcon( iconId, &icon_rect, true );
  131.         ClosePort((GrafPtr)&gp);
  132.     }
  133.     
  134.     if (advance)
  135.         Next_position(); /* JWW */
  136.  
  137.     SetA5(oldA5);
  138. }
  139.  
  140. /*
  141.     ShowInit's information is nestled at the tail end of CurApName.
  142.     It consists of a short which encodes the next horizontal offset,
  143.     and another short which is that value checksummed with the function below.
  144.  */
  145.  
  146. #define CurApName_LM    0x910
  147. #define ShowINITTable ((unsigned short*)(CurApName_LM + 32 - 4))
  148. #define CheckSumConst 0x1021        /* magic value to check-sum with. */
  149.  
  150. #define InitialXPosition 8            /* initial horizontal offset. */
  151. #define YOffset            40            /* constant from bottom to place the icon. */
  152. #define XOffset            40            /* amount to change it by. */
  153. #define ICON_WIDTH        32
  154. #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
  155.  
  156. /* CheckSum() computes the magic value to determine if ShowInit's have run already. */
  157.  
  158. static short CheckSum(register unsigned short x)
  159. {
  160. //    asm {
  161. //        rol.w    #1, x
  162. //    }
  163.     if (x & 0x8000)    /* high bit set */
  164.         return((x << 1) ^ CheckSumConst ^ 0x01);
  165.     else
  166.         return ((x << 1) ^ CheckSumConst);
  167. }
  168.  
  169. /*
  170.     GetIconRect() generates an appropriate rectangle to display the
  171.     next INIT's icon in.
  172.     It is also responsible for updating the horizontal
  173.     position in low memory.  This is a departure from
  174.     the original ShowInit code, which updates low
  175.     memory AFTER displaying the icon. -- changed by JWW
  176.     This code won't generate an icon position until it is certain that the icon can be loaded, so the
  177.     same behaviour occurs.
  178.     
  179.     This routine also generates a rectangle which is guaranteed to be onscreen.  It
  180.     does this by taking the horizontal offset modulo the screen width to generate
  181.     the horizontal position of the icon, and the offset divided by the screen
  182.     width to generate the proper row.
  183.  */
  184.  
  185. static void GetIconRect(register Rect* iconRect, Rect *screen_rect )
  186. {
  187.     register short screenWidth;
  188.     
  189.     screenWidth = screen_rect->right - screen_rect->left;
  190.     screenWidth -= screenWidth % XOffset;
  191.     /* if we are the first INIT to run we need to initialize the horizontal value. */
  192.     if (CheckSum(ShowINITTable[0]) != ShowINITTable[1])
  193.         ShowINITTable[0] = InitialXPosition;
  194.     
  195.     /* compute top left of icon's rect. */
  196.     iconRect->left = (ShowINITTable[0] % screenWidth);
  197.     iconRect->top = screen_rect->bottom -
  198.         YOffset * (1 + (ShowINITTable[0] / screenWidth));
  199.     iconRect->right = iconRect->left + 32;
  200.     iconRect->bottom = iconRect->top + 32;
  201.     
  202. }
  203.  
  204. /*
  205.     JWW: In Beard's original version, this was done at the end of
  206.     GetIconRect. That caused incorrect behavior when IconWrap 1.2 was
  207.     used to wrap icons.  Namely, if an INIT using that version of
  208.     ShowIconFamily was the first in a row, then the second icon in that
  209.     row would land on top of it.
  210. */
  211. static void Next_position( void )
  212. {
  213.     /* advance the position for the next icon. */
  214.     ShowINITTable[0] += XOffset;
  215.     
  216.     /* recompute the checksum. */
  217.     ShowINITTable[1] = CheckSum(ShowINITTable[0]);    
  218. }
  219.  
  220. /* DrawBWIcon() draws the 'ICN#' member of the icon family. */
  221.  
  222. void DrawBWIcon( short iconId, Rect *icon_rect, Boolean visible )
  223. {
  224.     Handle        icon;
  225.     BitMap        source, destination;
  226.     RgnHandle    empty_mask;
  227.     GrafPtr        port;
  228.     
  229.     icon = Get1Resource('ICN#', iconId);
  230.     if (!icon)
  231.         return;
  232.     HLock(icon);
  233.     
  234.     /* prepare the source and destination bitmaps. */
  235.     source.baseAddr = *icon + 128;                    /* mask address. */
  236.     source.rowBytes = 4;
  237.     SetRect(&source.bounds, 0, 0, 32, 32);
  238.     GetPort( &port );
  239.     destination = ((GrafPtr)port)->portBits;
  240.     empty_mask = visible? nil : NewRgn();
  241.     
  242.     /* transfer the mask. */
  243.     CopyBits(&source, &destination, &source.bounds, icon_rect,
  244.         srcBic, empty_mask);
  245.     
  246.     /* and the icon. */
  247.     source.baseAddr = *icon;    
  248.     CopyBits(&source, &destination, &source.bounds, icon_rect,
  249.         srcOr, empty_mask);
  250.     
  251.     if (empty_mask) DisposeRgn( empty_mask );
  252. }
  253.